#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <string>
#include <iostream>

using namespace std;

int sockfd;
unsigned long long count=0;
void sig_int(int sig)
{

    exit(0);
}

int main( int argc, char* argv[] )
{
    //signal(SIGINT,sig_int);
    const char* ip = "127.0.0.1";
    int port = 13370;

    struct sockaddr_in server_address;
    bzero( &server_address, sizeof( server_address ) );
    server_address.sin_family = AF_INET;
    inet_pton( AF_INET, ip, &server_address.sin_addr );
    server_address.sin_port = htons( port );

    sockfd = socket( PF_INET, SOCK_STREAM, 0 );
    assert( sockfd >= 0 );
    if ( connect( sockfd, ( struct sockaddr* )&server_address, sizeof( server_address ) ) < 0 )
    {
        printf( "connection failed\n" );
    }
    else
    {
        string json2="{\"type\":0,\"room\":\"101\",\"switch\":1,\"temperature\":25.70,\"wind\":1}";
        int size2=json2.size()+1;
        size2=htonl(size2);
        send(sockfd,&size2,sizeof(int),0);
        send(sockfd,json2.c_str(),json2.size()+1,0);
        sleep(1);
        while(count<100)
        {
            string tmp="{\"type\":1,\"room\":\"101\",\"temperature\":";
            string json1=tmp+std::to_string(40-0.2*count)+string("}");
            //string json1="{\"type\":1,\"room\":\"101\",\"temperature\":24.8}";
            int size1=json1.size()+1;
            size1=htonl(size1);
            send(sockfd,&size1,sizeof(int),0);
            send(sockfd,json1.c_str(),json1.size()+1,0);
            sleep(1);
            count++;
        }
    }
    shutdown( sockfd ,SHUT_RDWR);
    return 0;
}
